import axios from 'axios'
import qs from 'qs'

// 创建一个axios对象
let http = axios.create({
  baseURL: 'http://localhost:8081',
  timeout: 1000000000,
  method: 'POST'
  // 是否携带凭证
  // withCredentials: true
})

http.interceptors.request.use(config => {
  // 请求方式是post

  // eslint-disable-next-line eqeqeq
  if (config.method === 'post' || config.method === 'POST') {
    config.data = qs.stringify(config.data)
    config.headers['content-Type'] = 'application/x-www-form-urlencoded'
  }
  // 获取token值
  let token = sessionStorage.getItem('token')
  // 请求携带token值
  if (token) {
    console.log(1)
    config.headers.token = token
  }
  return config
})

http.interceptors.response.use(response => {
  // console.log('拦截响应' + response)
  return response.data
})

export default http
